package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.event.annotation.PrepareTestInstance;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;

    @Reference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String findPage(HttpServletRequest request, Model model){
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }

    @GetMapping("/create")
    public String create(){
        return "admin/create";
    }

    @PostMapping("/save")
    public String save(Admin admin){
        admin.setHeadUrl("https://img1.doubanio.com/view/group_topic/l/public/p515017570.webp");
        //进行密码加密处理
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @PostMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id,Model model){
        //还是转发到一个单独的上传页面,把id也带过去
        model.addAttribute("id",id);
        return "admin/upload";
    }

    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file) throws IOException {
        //上传图片到七牛云
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        String fileName= UUID.randomUUID().toString()+suffix;
        QiniuUtil.upload2Qiniu(file.getBytes(),fileName);
        //上传图片信息到数据库
        Admin admin = new Admin();
        admin.setHeadUrl("http://skizgp4uh.hn-bkt.clouddn.com/"+fileName);
        admin.setId(id);
        adminService.update(admin);

        return "common/successPage";
    }

    /**
     * 进入分配角色页面
     * @param adminId
     * @return
     */
    @GetMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId,Model model){
        //根据adminId查询到分配好的角色列表以及未分配的角色列表
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        //因为这里用的是thymeleaf技术,需要存到共享域里转发渲染到角色管理页面
        model.addAllAttributes(roleMap);
        //还需要把当前用户ID传递到角色管理页面,那页面有改动会根据ID进行后台处理/
        model.addAttribute("adminId",adminId);
        return "admin/assignShow";
    }

    /**
     * 根据用户分配角色
     * @param adminId
     * @param roleIds   前端传入的是"1,7,9,"这样的数据,这样的数据可以用数组接收,但注意,数组最后是有一位空的
     * @return
     */
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){
        //保存用户的角色
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }

    /**
     * 获取当前登录信息  个人理解这个方法用途是方便调试以及可能未来前端会需要json的用户信息
     * @return
     */
    @GetMapping("getInfo")
    @ResponseBody
    public Object getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        return authentication.getPrincipal();
    }


}
